package cn.itsource.yaoyuan.controller;

import cn.itsource.yaoyuan.util.AjaxResult;
import cn.itsource.yaoyuan.util.FastDfsApiOpr;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class FileController {

    @PostMapping("fastdfs")
    public AjaxResult upload(MultipartFile file){
        try {
            //获取文件扩展名
            String filename = file.getOriginalFilename();
            String extName = filename.substring(filename.lastIndexOf(".") + 1);
            String file_id = FastDfsApiOpr.upload(file.getBytes(), extName);
            return AjaxResult.me().setSuccess(true).setMessage("上传成功!").setRestObj(file_id);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上传失败!"+e.getMessage());
        }

    }
}
